package com.hnucm.c202001020216.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;
import com.hnucm.c202001020216.DataApplication;
import com.hnucm.c202001020216.R;
import com.hnucm.c202001020216.api.CarApi;
import com.hnucm.c202001020216.databinding.ActivityFindCarBinding;
import com.hnucm.c202001020216.databinding.KeywordcaritemBinding;
import com.hnucm.c202001020216.pojo.Car;
import com.hnucm.c202001020216.utils.RetrofitUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class FindCarActivity extends AppCompatActivity {
    String keyWord="玛莎拉蒂";
    ActivityFindCarBinding findCarBinding;
    MyAdapter myAdapter;
    RecyclerView recyclerView;
    ImageView findBtn;
    EditText findEdt;
    public List<Car.PageDTO.ListDTO> carList=new ArrayList<>();
    public Car car=new Car();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findCarBinding = DataBindingUtil.setContentView(this, R.layout.activity_find_car);
        recyclerView = findCarBinding.recyclerView4;
        findBtn = findCarBinding.imageView33;

        findEdt = findCarBinding.editTextTextPersonName2;

        ImmersionBar.with(this)
                .transparentStatusBar()
                .statusBarDarkFont(true)
                .init();

        Toast.makeText(FindCarActivity.this, "搜索词" + keyWord, Toast.LENGTH_SHORT).show();


        car = (Car) getIntent().getSerializableExtra("allCar");

        if (car == null) {
            Toast.makeText(FindCarActivity.this, "keyCar is null", Toast.LENGTH_SHORT).show();
        } else {
            carList.addAll(car.page.list);
            myAdapter = new MyAdapter();
            recyclerView.setAdapter(myAdapter);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            findBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    keyWord = findEdt.getText().toString().trim();
//                keyWord="宝马";
                    CarApi api = RetrofitUtils.getRetrofit(DataApplication.HttpURL).create(CarApi.class);
                    Call<Car> itemCall = api.getAllCar("奥迪");
                    itemCall.enqueue(new Callback<Car>() {
                        @Override
                        public void onResponse(Call<Car> call, Response<Car> response) {
                            car = response.body();
                            if (car == null) {
                                Toast.makeText(FindCarActivity.this, "car is null", Toast.LENGTH_LONG).show();
                            } else {
                                carList.clear();
                                carList.addAll(car.page.list);

                                myAdapter.notifyDataSetChanged();
                            }

                        }

                        @Override
                        public void onFailure(Call<Car> call, Throwable t) {

                        }
                    });
//                myAdapter=new MyAdapter();
//                recyclerView.setAdapter(myAdapter);
//                recyclerView.setLayoutManager(new LinearLayoutManager(FindCarActivity.this));
                }
            });
        }
    }


    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder>{
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            KeywordcaritemBinding keywordcaritemBinding=DataBindingUtil.inflate(LayoutInflater.from(FindCarActivity.this),
                    R.layout.keywordcaritem,parent,false);
            MyViewHolder myViewHolder= new MyViewHolder(keywordcaritemBinding.getRoot());

            myViewHolder.keywordcaritemBinding=keywordcaritemBinding;
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            final Car.PageDTO.ListDTO carKey=carList.get(position);
            holder.keywordcaritemBinding.setCar(carKey);
            //按钮监听
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(FindCarActivity.this,LookCarActivity.class);
//                    将被点击的参数信息给下一个界面
                    intent.putExtra("keyCar",carKey);
                    startActivity(intent);
                    Toast.makeText(view.getContext(), "你选择的是"+carKey.carName
                    ,Toast.LENGTH_LONG).show();

                }
            });
        }

        @Override
        public int getItemCount() {
            return carList.size();
        }
    }
    public  class MyViewHolder extends RecyclerView.ViewHolder{
        KeywordcaritemBinding keywordcaritemBinding;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}



